package cn.com.wantools.service.provider;

import cn.com.wantools.model.exchange.ExchangeInfo;
import cn.com.wantools.model.loanrate.LoanRateInfo;
import cn.com.wantools.service.LoanRateService;
import cn.com.wantools.service.UcLoanRateService;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jfinal.aop.Inject;
import com.jfinal.kit.LogKit;
import io.jboot.aop.annotation.Bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Bean
public class LoanRateServiceProvider implements LoanRateService {

    @Inject
    private UcLoanRateService ucLoanRateService;

    @Override
    public List<LoanRateInfo> getLoanRate() {
        return loadLoanRate();
    }

    @Override
    public void reloadLoanRate() {
        loadLprLoanRate();
    }

    private void loadLprLoanRate() {
        LogKit.warn("loadLprLoanRate start!");
        String responseString= HttpUtil.get("https://www.chinamoney.com.cn/r/cms/www/chinamoney/data/currency/bk-lpr.json?t=" + System.currentTimeMillis());
        JSONObject jsonObject = JSONUtil.parseObj(responseString);
        Long currentTimestamp = System.currentTimeMillis();
        List<LoanRateInfo> loanRateInfoList = new ArrayList<>();
        JSONArray dataArray = jsonObject.getJSONArray("records");
        dataArray.toList(JSONObject.class).forEach(item -> {
            if ("1Y".equals(item.getStr("termCode"))) {
                LoanRateInfo loanRateInfo = new LoanRateInfo();
                loanRateInfo.setGmtCreate(currentTimestamp);
                loanRateInfo.setLeYear(5);
                loanRateInfo.setGtYear(0);
                loanRateInfo.setRemark("");
                loanRateInfo.setType("lpr");
                loanRateInfo.setLoanRate(item.getStr("shibor"));
                loanRateInfoList.add(loanRateInfo);
            } else if ("5Y".equals(item.getStr("termCode"))) {
                LoanRateInfo loanRateInfo = new LoanRateInfo();
                loanRateInfo.setGmtCreate(currentTimestamp);
                loanRateInfo.setLeYear(30);
                loanRateInfo.setGtYear(5);
                loanRateInfo.setRemark("");
                loanRateInfo.setType("lpr");
                loanRateInfo.setLoanRate(item.getStr("shibor"));
                loanRateInfoList.add(loanRateInfo);
            }
        });

        LogKit.warn("loadLprLoanRate loanRateInfoList: " + com.alibaba.fastjson.JSONObject.toJSONString(loanRateInfoList));

        List<LoanRateInfo> loanRateInfoListFromDb = ucLoanRateService.queryLoanRateByType("lpr");
        boolean inDb = true;
        if (loanRateInfoListFromDb == null) {
            inDb = false;
        } else {
            Map<String, LoanRateInfo> mapFromDb = loanRateInfoListFromDb.stream().collect(Collectors.toMap(e -> e.getType() + e.getGtYear() + e.getLeYear(), e -> e));
            for (LoanRateInfo loanRateInfo : loanRateInfoList) {
                LoanRateInfo item = mapFromDb.get(loanRateInfo.getType() + loanRateInfo.getGtYear() + loanRateInfo.getLeYear());
                if (item == null || !loanRateInfo.getLoanRate().equals(item.getLoanRate())) {
                    inDb = false;
                    break;
                }
            }
        }

        if (!inDb) {
            for (LoanRateInfo item: loanRateInfoList) {
                ucLoanRateService.saveLoanRate(item);
            }
        }
    }


    private List<LoanRateInfo> loadLoanRate() {
        return ucLoanRateService.queryAllLoanRate();
    }
}
